﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.segments.data_type;

namespace ConsoleApplication1
{
    class msh
    {
        public string Encoding_Characters { get; set; }
        public HD Sending_Application { get; set; }
        public HD Sending_Facility { get; set; } 
        public HD Receiving_Application { get; set; } 
        public HD Receiving_Facility { get; set; } 
        public string date { get; set; } 
        public CM message_type { get; set; }
        public StringBuilder message_control_id { get; set; } 
        public char Processing_ID { get; set; }
        public StringBuilder version_id { get; set; }
        public bool required { get; set; }
        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }
       

        public msh()
        {
            Sending_Application = new HD();
            Sending_Facility = new HD();
            Receiving_Application = new HD();
            Receiving_Facility = new HD();
            message_type = new CM();
            message_control_id = new StringBuilder();
            version_id = new StringBuilder();
        }
        

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                    return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                    return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                    return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                    return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                    return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                    return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                    return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                    return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                    return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                    return segments.RXO.ToString();
           
            return null;
        }

        public int fill(string source,int temp)
        {
            int i;
            int x = 0;
            for (i = temp; i < source.Length; i++)
            {

                if (check(source, i) != null && message_type!=null)
                {
                    message_control_id.Capacity = message_control_id.Length;
                    version_id.Capacity = version_id.Length;
                    return i - 1;
                }
                else if (source[i] == '|')
                {                      
                        x++;
                        continue;
                    
                }
                else
                {
                    if (x == 1)  // passing prameter of  encoding characters 
                        for (int z = i; ; z++)
                        {
                            if (source[z] != '|')
                                Encoding_Characters += source[z];
                            else
                            {
                                i = z;
                                x++;
                                break;
                            }
                        }
                    else if (x == 2) //passing prameter of sending application
                    {
                        if (source[i] != '|')
                        {
                            i = Sending_Application.fill(source, i);
                            x++;
                        }                      
                    }
                    else if (x == 3) // passing sending facility
                    {
                        if (source[i] != '|')
                        {
                            i = Sending_Facility.fill(source, i);
                            x++;
                        }                       
                    }

                    else if (x == 4) // passing reciving application
                    {
                        if (source[i] != '|')
                        {
                            i = Receiving_Application.fill(source, i);
                            x++;
                        }                     
                    }

                    else if (x == 5) // passing reciveing facility
                    {
                        if (source[i] != '|')
                        {
                            i = Receiving_Facility.fill(source, i);
                            x++;
                        }                      
                    }
                    else if (x == 6) // passing data prameter
                    {
                        if (source[i] != '|')
                            date += source[i];                     
                    }
                    else if (x == 7)
                    {
                        while (true)
                        {
                            if (source[++i] == '|')
                            {
                                x++;
                                break;
                            }
                        }
                    }
                    else if (x == 8)// passing message type prameter
                    {
                        if (source[i] != '|')
                        {
                            i = message_type.fill(source, i);
                            x++;
                        }                     
                    }

                    else if (x == 9) // passing mesage control id
                    {
                        if (source[i] != '|')
                            message_control_id.Insert(message_control_id.Length, source[i]);                       
                    }
                    else if (x == 10) // passing processing id
                    {
                        if (source[i] != '|')
                            Processing_ID = source[i];                      
                    }
                    else if (x == 11) // passing version id
                    {
                        
                      if (source[i] != '|')
                            version_id.Insert(version_id.Length, source[i]);                       
                    }

                }   
            }
            return i;
        }

        public string tostring()
        {
            string msh_string = "MSH";

            if (Encoding_Characters != null)
                msh_string +="|"+ "^~\\&";//Encoding_Characters.Remove(2,0);
            if (Sending_Application != null)
                msh_string += "|" + Sending_Application.tostring();
            if (Sending_Facility != null)
                msh_string += "|" + Sending_Facility.tostring();
            if (Receiving_Application!=null)
                msh_string+= "|" +Receiving_Application.tostring();
            if (Receiving_Facility != null)
                msh_string += "|" + Receiving_Facility.tostring();
            if (date != null)
                msh_string += "|" + date;
            if (message_type != null)
                msh_string += "|" + message_type.tostring();
            if (message_control_id != null)
                msh_string += "|" + message_control_id;
            if (Processing_ID != null)
                msh_string += "|" + Processing_ID.ToString();
            if (version_id != null)
                msh_string += "|" + version_id;

            return msh_string;
        }

    }
}
